package com.music.controller.music;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.music.dto.music.PlaylistDTO;
import com.music.dto.system.TokenDTO;
import com.music.dto.utils.OpResultDTO;
import com.music.dto.utils.TableReqDTO;
import com.music.dto.utils.TableRspDTO;
import com.music.service.music.PlaylistService;
import com.music.utils.HttpRequestReader;
import com.music.utils.JWTUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

@RestController
@RequestMapping("/api/playlist")
public class PlaylistController {
    @Resource
    private PlaylistService playlistService;
    private final Logger logger = LoggerFactory.getLogger(PlaylistController.class);

    /**
     * @Description: 获取数据列表
     * @Author dy
     * @Date 2021/11/21
     */
    @RequestMapping(value = "/queryByUserId", method = RequestMethod.POST)
    public TableRspDTO listPlaylistByUserId(@RequestHeader(name = "Access-Token") String token, @RequestBody TableReqDTO tableReqDTO) {
        TableRspDTO tableRspDTO = new TableRspDTO();
        try {
            TokenDTO tokenDTO = JWTUtil.verifyToken(token);
            tableRspDTO = playlistService.listPlaylistByUserId(tokenDTO.getAccountId(), tableReqDTO);
        } catch (Exception e) {
            logger.error(e.toString());
        }
        return tableRspDTO;
    }

    /**
     * @Description: 新增记录
     * @Author dy
     * @Date 2021/11/21
     */
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public OpResultDTO add(@RequestHeader(name = "Access-Token") String token, @RequestBody PlaylistDTO playlistDTO) {
        OpResultDTO opResult = new OpResultDTO();
        try {
            TokenDTO tokenDTO = JWTUtil.verifyToken(token);
            opResult.setMessage(playlistService.add(tokenDTO.getAccountId(), playlistDTO).toString());
        } catch (Exception e) {
            logger.error(e.toString());
            opResult.setMessage("error");
        }
        return opResult;
    }

    /**
     * @Description: 更新记录
     * @Author dy
     * @Date 2021/11/21
     */
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public OpResultDTO update(@RequestBody PlaylistDTO playlistDTO) {
        OpResultDTO opResult = new OpResultDTO();
        try {
            opResult.setMessage(playlistService.update(playlistDTO).toString());
        } catch (Exception e) {
            logger.error(e.toString());
            opResult.setMessage("error");
        }
        return opResult;
    }

    /**
     * @Description: 删除记录
     * @Author dy
     * @Date 2021/11/21
     */
    @RequestMapping(value = "/remove", method = RequestMethod.POST)
    public OpResultDTO remove(@RequestBody PlaylistDTO playlistDTO) {
        OpResultDTO opResult = new OpResultDTO();
        try {
            opResult.setMessage(playlistService.remove(playlistDTO).toString());
        } catch (Exception e) {
            logger.error(e.toString());
            opResult.setMessage("error");
        }
        return opResult;
    }

    /**
     * @Description: 获取数据列表
     * @Author hbh
     * @Date 2021/11/26
     */
    @RequestMapping(value = "/queryMusicSquareByUserId", method = RequestMethod.POST)
    public TableRspDTO listMusicSquareByUserId(@RequestHeader(name = "Access-Token") String token, @RequestBody TableReqDTO tableReqDTO) {
        TableRspDTO tableRspDTO = new TableRspDTO();
        try {
            TokenDTO tokenDTO = JWTUtil.verifyToken(token);
            tableRspDTO = playlistService.listMusicSquareByUserId(tokenDTO.getAccountId(), tableReqDTO);
        } catch (Exception e) {
            logger.error(e.toString());
        }
        return tableRspDTO;
    }

    /**
     * @Description: 获取歌单下载次数
     * @Author dy
     * @Date 2021/12/26
     */
    @RequestMapping(value = "/getDownloadSum", method = RequestMethod.POST)
    public OpResultDTO getDownloadSum(HttpServletRequest request) {
        OpResultDTO opResult = new OpResultDTO();
        try {
            JSONObject jsonObject = HttpRequestReader.getJsonObject(request);
            Long playlistId = jsonObject.getLong("playlistId");
            opResult.setResult(playlistService.getDownloadSum(playlistId));
            opResult.setMessage("success");
        } catch (Exception e) {
            logger.error(e.toString());
            opResult.setMessage("error");
        }
        return opResult;
    }

    /**
     * @Description: 获取歌单下载次数
     * @Author dy
     * @Date 2021/12/26
     */
    @RequestMapping(value = "/updateTagByPlaylist", method = RequestMethod.POST)
    public OpResultDTO updateTagByPlaylist(HttpServletRequest request) {
        OpResultDTO opResult = new OpResultDTO();
        try {
            JSONObject jsonObject = HttpRequestReader.getJsonObject(request);
            Long playlistId = jsonObject.getLong("playlistId");
            String tagIds = jsonObject.getString("tagIds");
            opResult.setResult(playlistService.updateTagByPlaylist(playlistId, tagIds));
            opResult.setMessage("success");
        } catch (Exception e) {
            logger.error(e.toString());
            opResult.setMessage("error");
        }
        return opResult;
    }
}
